var vm = new Vue({
    el: '#rrapp',
    data: {
        qFreeze: {isFreeze: 0, orderAccountId: null, orderId: null, orderCreateTime: null, receivedTime: null},
        q: {isFreeze: 1, orderAccountId: null, orderId: null, orderCreateTime: null, receivedTime: null},
        page: {currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0, ascending: [], descending: []},
        pageFreeze: {currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0},
        title: null,
        activeName: 'second',
        merchantApplyCash: {},
        applyCashRule:{},
        orderAccountFreezeData: [],//冻结订单
        orderAccountData: [],// 可提现订单
    },
    mounted: function () {
        this.query();
    },
    methods: {
        //tab 页切换
        handleClick: function (tab, event) {
            if(tab.name=='first'){ //冻结订单
                this.queryFreeze();
            }
            if(tab.name=='second'){ //可提现订单
                this.query();
            }
        },
        // 冻结页数变化
        handleFreezeSizeChange: function (val) {
            this.pageFreeze.pageSize = val;
            this.queryFreeze();
        },
        //冻结当前页变化
        handleFreezeCurrentChange: function (val) {
            this.pageFreeze.currentPage = val;
            this.queryFreeze();
        },

        // 页数变化
        handleSizeChange: function (val) {
            this.page.pageSize = val;
            this.query();
        },
        //当前页变化
        handleCurrentChange: function (val) {
            this.page.currentPage = val;
            this.query();
        },
        // 查询
        query: function () {
            //查询参数
            var queryParam = this.q;

            // 分页参数
            queryParam.page = this.page.currentPage;
            queryParam.limit = this.page.pageSize;

            //排序参数
            queryParam.ascending = this.page.ascending;
            queryParam.descending = this.page.descending;

            //ajax 请求
            $.ajax({
                type: "POST",
                url: baseURL + "capital/merchantorderaccount/list",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(queryParam),
                success: function (r) {
                    if (r.code == 0) {
                        vm.applyCashRule = r.applyCashRule;
                        vm.page.total = r.page.totalCount;
                        vm.orderAccountData = r.page.list;
                    } else {
                        alert(r.msg);
                    }
                }
            });


        },

        sortChange: function (column) {
            if (column.order === 'ascending'){
                if (this.page.ascending.indexOf(column.column.index)== -1){
                    this.page.ascending.push(column.column.index);
                }
                if (this.page.descending.indexOf(column.column.index)> -1){
                    this.page.descending.splice(this.page.descending.indexOf(column.column.index),1);
                }
            }
            if (column.order === 'descending') {
                if (this.page.descending.indexOf(column.column.index)== -1){
                    this.page.descending.push(column.column.index);
                }
                if (this.page.ascending.indexOf(column.column.index)> -1){
                    this.page.ascending.splice(this.page.ascending.indexOf(column.column.index),1);

                }
            }

            if (column.order === null) {
                if (this.page.descending.indexOf(column.column.index)>-1){
                    this.page.descending.splice(this.page.descending.indexOf(column.column.index),1);
                }
                if (this.page.ascending.indexOf(column.column.index)>-1){
                    this.page.ascending.splice(this.page.ascending.indexOf(column.column.index),1);
                }

            }

            this.query();
        },

        // 冻结查询
        queryFreeze: function () {
            //查询参数
            var queryParam = this.qFreeze;

            // 分页参数
            queryParam.page = this.pageFreeze.currentPage;
            queryParam.limit = this.pageFreeze.pageSize;

            //ajax 请求
            $.ajax({
                type: "POST",
                url: baseURL + "capital/merchantorderaccount/list",
                contentType: "application/json",
                data: JSON.stringify(queryParam),
                success: function (r) {
                    if (r.code == 0) {
                        vm.pageFreeze.total = r.page.totalCount;
                        vm.orderAccountFreezeData = r.page.list;
                    } else {
                        alert(r.msg);
                    }
                }
            });


        },
        tableRowClassName({row, rowIndex}) {
            if (row.orderAllotId){ // 分配订单
                return 'red-row';
            }
            return '';
        },

        //申请提现
        applyCash: function () {
            // 取出当前用户的 商家类型 0供应商 1 工厂
            var merchantType =  window.parent.vm.user.merchantType;
            if (merchantType === "" || merchantType === null || merchantType === undefined) {
                this.$message.error('商户类型异常，请刷新页面重试 !');
                return;
            }

            //获取选中的订单
            var selected = this.$refs.orderAccountTable.selection;
            if (selected.length == 0) {
                this.$message.error('请选择提现订单 !');
                return;
            }
            var flag = true;
            var totalMoney = 0;
            var orderAccountIds = [];
            //循环 选中订单
            var gtDay = []; //大于指定日期订单数
            var eltDay = []; // 小于等于指定日期订单数
            for (var index in selected) {
                var fundState = selected[index].fundState;
                if (fundState != 0) {
                    this.$message.error('订单【' + selected[index].orderAccountId +"】 状态不允许提现 !" );
                    flag = false;
                    return false;
                }

                /*
                   奖励金政策发生变化， 提现规则响应变化
                   1.2018.11.16: 23:59:59  之前的订单 按照 商品总额+运费+随机立减 计算提现金额
                   2.2018.11.16: 23:59:59  之后的订单 按照 商品总额+运费 计算提现金额
                   3.一次申请提现 下单日期不能夸 2018.11.16 天
                 */
                // 判断日期 与 2018.11.16: 23:59:59 比较
                var orderCreateTime = new Date(selected[index].orderCreateTime);
                var specifiedTime = new Date('2018-11-16 23:59:59');
                if(orderCreateTime.getTime() > specifiedTime.getTime()){
                    gtDay.push(index);
                }else {
                    eltDay.push(index);
                }
                if(gtDay.length>0 && eltDay.length>0){
                    alert( '2018年11月16日24:00之后的金额需单独提现 !<br> 订单【' + selected[index].orderAccountId +'】');
                    flag = false;
                    return false;
                }

                // 判断订单是否分配订单 0: 不是 1: 是
                var isAllot = 0;
                if (selected[index].orderAllotId){
                    isAllot = 1;
                }

                if (merchantType === 0 && isAllot === 0){ // 商户非分配订单
                    if(eltDay.length >0){
                        //商家提现 计算可提现金额 商品总额 + 运费 + 随机立减
                        var lineAmount = selected[index].totalAmount + selected[index].costFreight + selected[index].pmtRandom;
                        totalMoney += lineAmount;
                    }
                    if (gtDay.length >0){
                        //商家提现 计算可提现金额 商品总额 + 运费
                        var lineAmount = selected[index].totalAmount + selected[index].costFreight;
                        totalMoney += lineAmount;
                    }

                }

                if (merchantType === 1 || isAllot === 1){ // 工厂或者分配订单
                    //工厂提现 计算可提现金额 商品总额 + 运费 + 调价
                    var lineAmount = selected[index].totalAmount + selected[index].costFreight + selected[index].discount;
                    totalMoney += lineAmount;
                }
                orderAccountIds.push(selected[index].orderAccountId);

            }
            if (!flag) {
                return;
            }
            var self = this;
            // 提示框 准备提现
            totalMoney = totalMoney.toFixed(3);
            this.$confirm('确认提现 ? 提现金额:'+totalMoney, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //ajax 提交 请求提现
                $.ajax({
                    type: "POST",
                    url: baseURL + "capital/merchantapplycash/applyCash",
                    contentType: "application/json",
                    data: JSON.stringify({totalMoney : totalMoney, orderAccountIds: orderAccountIds}),
                    success: function (r) {
                        if (r.code == 0) {
                            self.$message({
                                type: 'success',
                                message: '提现成功!'
                            });
                            self.query();
                        } else {
                            alert(r.msg);
                        }
                    }
                });

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消提现'
                });
            });
        },

    },


});